package cn.edu.scau.cmi.chenyipeng.composite.safe;

import java.net.URL;
import java.util.Iterator;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;

public class SafeCompositeController {

	private TreeItem <SafeComponent> rootItem;
	private SafeComponent root;
	
    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TextField nameTF;

    @FXML
    private TreeView<SafeComponent> treeView;

    @FXML
    private Button createRoot;
    
    @FXML
    private Button addTeam;

    @FXML
    private Button addPerson;

    @FXML
    private Button delete;
    
    @FXML 
    private void createRootItem() {
    	root = new SafeComposite(nameTF.getText());
    	rootItem = new TreeItem(root);
    	//rootItem = new TreeItem(nameTF.getText());
    	rootItem.setExpanded(true);
    	showTreeView();
    	
    }
    
    @FXML
    private void addTeamItem() {
    	//System.out.println("1");
    	TreeItem chosenItem = treeView.getSelectionModel().getSelectedItem();
    	if(chosenItem.getValue() instanceof SafeComposite && nameTF.getText().compareTo("")!=0) {
    		//判断被插入节点是否为团队节点且输入团队名不能为空
    		SafeComponent tempTeam = new SafeComposite(nameTF.getText());
    		((SafeComposite)tempTeam).setParent(((SafeComponent)chosenItem.getValue()));
    		TreeItem tempTeamItem = new TreeItem(tempTeam);
    		tempTeamItem.setExpanded(true);
    		((SafeComposite)chosenItem.getValue()).addChild(tempTeam);
    		chosenItem.getChildren().add(tempTeamItem);
    	}
    	else {
    		//被插入节点不为团队或团队名为空就不能插入
    	}
    	//SafeComponent tempComposite = new SafeComposite(nameTF.getText());
    	/*System.out.println(((SafeComposite)rootItem.getValue()).getName());
    	System.out.println(((SafeComposite)rootItem.getValue()).getChildren().size());*/
    	showTreeView();
    }
    
    @FXML 
    private void addPersonItem() {
    	TreeItem chosenItem = treeView.getSelectionModel().getSelectedItem();
    	if(chosenItem.getValue() instanceof SafeComposite && nameTF.getText().compareTo("")!=0) {
    		SafeComponent tempPerson = new SafeLeaf(nameTF.getText());
    		((SafeLeaf)tempPerson).setParent(((SafeComponent)chosenItem.getValue()));
    		TreeItem tempPersonItem = new TreeItem(tempPerson);
    		((SafeComposite)chosenItem.getValue()).addChild(tempPerson);
    		chosenItem.getChildren().add(tempPersonItem);
    	}
    	else {
    		
    	}
    	showTreeView();
    }
    
    @FXML 
    private void deleteItem() {
    	TreeItem chosenItem = treeView.getSelectionModel().getSelectedItem();
    	TreeItem chosenParentItem = chosenItem.getParent();
    	if(chosenItem.getValue() instanceof SafeLeaf) {
    		SafeComponent tempParent = ((SafeLeaf)(chosenItem.getValue())).getParent();
    		//System.out.println(((SafeComposite)tempParent).getChildren().size());
        	((SafeComposite)tempParent).removeChild(((SafeLeaf)chosenItem.getValue()));
        	chosenParentItem.getChildren().remove(chosenItem);
        	/*TreeItem newParentItem = new TreeItem(tempParent);
        	Iterator<SafeComponent> interator
        		= ((SafeComposite)tempParent).getChildren().iterator();
        	while(interator.hasNext()) {
        		newParentItem.getChildren().add(new TreeItem(interator.next()));
        	}*/
        	//System.out.println(((SafeComposite)tempParent).getChildren().size());
        	//chosenParentItem = new TreeItem(tempParent);
        	//chosenItem = null;
    	}
    	else {
    		SafeComponent tempParent = ((SafeComposite)(chosenItem.getValue())).getParent();
    		if(tempParent != null) {
    			((SafeComposite)tempParent).removeChild(((SafeComposite)chosenItem.getValue()));
    			chosenParentItem.getChildren().remove(chosenItem);
    			//chosenParentItem = new TreeItem(tempParent);
    			//chosenItem = null;
    		}
    		else{
    			rootItem = null;
    		}
    	}
    	showTreeView();
    }
    
    @FXML 
    private void showTreeView() {
    	//treeView = new TreeView<SafeComponent>(rootItem);
    	treeView.setRoot(rootItem);
    }
    @FXML
    void initialize() {
        assert nameTF != null : "fx:id=\"nameTF\" was not injected: check your FXML file 'SafeCompositeUI.fxml'.";
        assert treeView != null : "fx:id=\"treeView\" was not injected: check your FXML file 'SafeCompositeUI.fxml'.";
        assert addTeam != null : "fx:id=\"addTeam\" was not injected: check your FXML file 'SafeCompositeUI.fxml'.";
        assert addPerson != null : "fx:id=\"addPerson\" was not injected: check your FXML file 'SafeCompositeUI.fxml'.";
        assert delete != null : "fx:id=\"delete\" was not injected: check your FXML file 'SafeCompositeUI.fxml'.";

    }
}

